package com.gui.reflection;

import com.gui.entity.Person;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Slf4j
public class ReflectApiDemo1 {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Class<ReflectApiDemo1> aClass = ReflectApiDemo1.class;
        Method[] methodList = aClass.getDeclaredMethods();
        for (Method method : methodList) {
            if ("main".equals(method.getName())) {
                continue;
            }
            method.setAccessible(true);
            method.invoke(null, new Object[0]);
            System.out.println(method.getName() + " 方法执行结束...");
        }
    }

    private static void template() {
        Class<Person> personClass = Person.class;
        try {
            Person person = personClass.getConstructor().newInstance();
            System.out.println(person);
        } catch (Exception e) {
            log.error("反射失败", e);
        }
    }

    private static void testCreateSimpleInstance() {
        // 利用反射创建对象实例
        Class<Person> personClass = Person.class;
        try {
            Person person = personClass.getConstructor().newInstance();
            System.out.println(person);
        } catch (Exception e) {
            log.error("创建对象实例失败", e);
        }
    }

    private static void testCreateConstructorInstance() {
        Class<Person> personClass = Person.class;
        try {
            Person person = personClass.getConstructor(int.class, String.class).newInstance(101, "Tom");
            System.out.println(person);
        } catch (Exception e) {
            log.error("创建对象实例失败", e);
        }
    }

    private static void getAllMethod() {
        // 获取所有的方法
        Class<Person> personClass = Person.class;
        try {
            for (Method method : personClass.getDeclaredMethods()) {
                System.out.println(method.getName());
            }
        } catch (Exception e) {
            log.debug("反射失败", e);
        }
    }

    private static void testInvokeMethod() {
        Person person = new Person(102, "神里绫华");
        try {
            Class<? extends Person> personClass = person.getClass();
            // 获取指定方法并调用
            Method setAgeMethod = personClass.getDeclaredMethod("setAge", int.class);
            setAgeMethod.setAccessible(true);
            setAgeMethod.invoke(person, 26);
            System.out.println(person);
        } catch (Exception e) {
            log.error("反射失败", e);
        }
    }

    private static void testModifyField() {
        Person person = new Person(102, "神里绫华");
        try {
            Class<? extends Person> personClass = person.getClass();
            // 获取指定对象属性并修改
            Field field = personClass.getDeclaredField("id");
            field.setAccessible(true);
            field.set(person, 202);
            System.out.println(person);
        } catch (Exception e) {
            log.error("反射失败", e);
        }
    }
}
